using System;
using System.Collections.Generic;
using System.Windows.Forms;
using System.Threading;
using System.IO;
using System.Diagnostics;
using System.Security.AccessControl;

namespace SPTraceView
{
    static class Program
    {
        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        [STAThread]
        static void Main()
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);

            Mutex mutex = null;
            bool createdNew = false;
            int secWaiting = 0;
            do
            {
                if (secWaiting > 0) Thread.Sleep(300);
                mutex = new Mutex(true, "~MUTEX~SPTRACEVIEW", out createdNew);

                if (secWaiting > 0) Thread.Sleep(500);
                secWaiting++;
            }
            while (
                !createdNew &&
                secWaiting < 6);

            if (!createdNew)
            {
                MessageBox.Show("Another instance of SPTraceView is already running. Only one instance is allowed.", "Only one instance is allowed", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }


            //Debugger.Launch();
            try
            { 
                frmMain frm = new frmMain();
                try
                {
                    Application.Run();
                }
                finally
                {
                    frm.Dispose();
                    frm = null;
                }
            }
            finally
            {
                if (createdNew)
                    mutex.ReleaseMutex();
            }
        }
    }
}